package com.ecarx.vhaltests.hy11.testcases.light;

import android.car.VehicleAreaType;
import android.car.hardware.CarPropertyValue;
import android.car.hardware.property.CarPropertyManager;
import android.util.Log;

import androidx.test.platform.app.InstrumentationRegistry;

import com.ecarx.vhaltests.IpcpClient;
import com.ecarx.vhaltests.VehicleConstants;
import com.ecarx.vhaltests.VhalUnitTestBase;
import com.ecarx.xui.adaptapi.car.Car;
import com.ecarx.xui.adaptapi.car.IWrapper;
import com.ecarx.xui.adaptapi.car.vehicle.IVehicle;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

public class LampAutomaticCourtesyLightTest extends VhalUnitTestBase {

    private static final String TAG = LampAutomaticCourtesyLightTest.class.getSimpleName();
    private static IWrapper.IPropertyId mWrappedPropertyId;
    private static final Object lock = new Object();
    private static boolean mCarPropertyValue;

    private static final CarPropertyManager.CarPropertyEventCallback mEventCallback = new CarPropertyManager.CarPropertyEventCallback() {
        @Override
        public void onChangeEvent(CarPropertyValue carPropertyValue) {

            Log.d(TAG, "onChangeEvent: [ propertyId = " + carPropertyValue.getPropertyId() +
                    " value = " + carPropertyValue.getValue() +
                    " status = " + carPropertyValue.getStatus() +
                    " areaId = " + carPropertyValue.getAreaId());
            mCarPropertyValue = (boolean) carPropertyValue.getValue();

            synchronized (lock) {
                lock.notify();
            }
        }

        @Override
        public void onErrorEvent(int i, int i1) {

        }
    };

    @BeforeClass
    public static void testInit() {
        IWrapper wrapper = Car.createWrapper(InstrumentationRegistry.getInstrumentation().getTargetContext());
        mWrappedPropertyId = wrapper.getWrappedPropertyId(IWrapper.WrappedIdType.ID_TYPE_FUNCTION, IVehicle.SETTING_FUNC_LAMP_AUTOMATIC_COURTESY_LIGHT);
        mCarPropertyMgr.registerCallback(mEventCallback, mWrappedPropertyId.getPropertyId(), CarPropertyManager.SENSOR_RATE_ONCHANGE);
    }

    @Test
    public void testCourtesyLightOn() {
        setActive();
        delay();
        mCarPropertyMgr.setBooleanProperty(mWrappedPropertyId.getPropertyId(), VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL, true);
        lockWait();
        Assert.assertTrue(mCarPropertyValue);
        boolean property = mCarPropertyMgr.getBooleanProperty(mWrappedPropertyId.getPropertyId(), VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL);
        Assert.assertTrue(property);
    }

    @Test
    public void testCourtesyLightOff() {
        setActive();
        delay();
        mCarPropertyMgr.setBooleanProperty(mWrappedPropertyId.getPropertyId(), VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL, false);
        lockWait();
        Assert.assertFalse(mCarPropertyValue);
        boolean property = mCarPropertyMgr.getBooleanProperty(mWrappedPropertyId.getPropertyId(), VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL);
        Assert.assertFalse(property);
    }

    private void setActive() {
        IpcpClient ipcpClient = new IpcpClient();
        int cemSvcId = 37;
        int[] cemOperIds = {999};
        ipcpClient.setConfig(cemSvcId, cemOperIds);
        String[] cems = {"vehModMngtGlbSafe1CarModSts1"};
        int[] cemValues = {VehicleConstants.CAR_MODE_NOMAL};
        boolean cemRet = ipcpClient.setCEMSignal(cemSvcId, cemOperIds[0], cems, cemValues);
        Assert.assertTrue(cemRet);
    }

    @AfterClass
    public static void handleCallbackRegister() {
        mCarPropertyMgr.unregisterCallback(mEventCallback, mWrappedPropertyId.getPropertyId());
    }

    private void lockWait() {
        synchronized (lock) {
            try {
                lock.wait(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
